# 
# #code republican, democrat, independent 
# data_panel$pid <- NA
# data_panel$pid[data_panel$pid_mod < 4] <- 1 #Democrat
# data_panel$pid[data_panel$pid_mod == 5 | data_panel$pid_mod == 6 | data_panel$pid_mod == 7] <- 2 #Republican
# data_panel$pid[data_panel$pid_mod == 4 ] <- 3 #Independent or Other
# data_panel$pid <- recode(data_panel$pid, `1` = "Democrat", `2`= "Republican", `3` = "Independent")
# data_panel$pid <- as.factor(data_panel$pid)
# data_panel$pid <-relevel(data_panel$pid, "Democrat") #code relative to independent
# 
# data_cross$pid <- NA
# data_cross$pid[data_cross$pid7 < 4] <- 1 #Democrat
# data_cross$pid[data_cross$pid7 == 5 | data_cross$pid7 == 6 | data_cross$pid7 == 7] <- 2 #Republican
# data_cross$pid[data_cross$pid7 == 4 ] <- 3 #Independent or Other
# data_cross$pid <- recode(data_cross$pid, `1` = "Democrat", `2`= "Republican", `3` = "Independent")
# data_cross$pid <- as.factor(data_cross$pid)
# data_cross$pid <-relevel(data_cross$pid, "Democrat")
# 
# 
# #store leak and decision cut variables
# data_panel <- mutate(data_panel,
#                      leak_cut = case_when(
#                        wave > 0 ~ 1, #post-leak
#                        TRUE ~ 0
#                      ))
# 
# data_panel <- mutate(data_panel,
#                      decision_cut = case_when(
#                        wave > 1 ~ 1, #post-decision
#                        TRUE ~ 0
#                      ))
# 
# data_cross <- mutate(data_cross,
#                      leak_cut = case_when(
#                        wave > 0 ~ 1, #post-leak
#                        TRUE ~ 0
#                      ))
# 
# data_cross <- mutate(data_cross,
#                      decision_cut = case_when(
#                        wave > 1 ~ 1, #post-decision
#                        TRUE ~ 0
#                      ))
# 
# ##### calculate days since variable #######
# #leak on 2022-05-02 , decision on 2022-06-24
# data_panel$dayssince_leak = as.Date(data_panel$date) - as.Date('2022-05-02')
# data_panel$dayssince_decision = as.Date(data_panel$date) - as.Date('2022-06-24')
# 
# data_cross$dayssince_leak = as.Date(data_cross$date) - as.Date('2022-05-02')
# data_cross$dayssince_decision = as.Date(data_cross$date) - as.Date('2022-06-24')
# 
# 
# ####### store dv's  #########
# dvs <- c('personal_roe', 'personal_dobbs', 'courtlegit_composite',
#          'approve_court', 'courtsize', 'courtterms',
#          'courtideology')
# 
# dvs2 <- c('norm', 'moral_norm', 'opinion', 'moral_opinion', 'fetal_scale')
# 
# dv_last <- c('lagged_personal_dobbs', 'lagged_personal_roe',
#              'lagged_courtlegit_composite',
#              'lagged_approve_court',
#              'lagged_courtsize', 'lagged_courtterms', 
#              'lagged_courtideology')
# 
# extras <- c('court_represent', 'personal_affirmative', 'oersonal_rifle',
#             'pride', 'political_interest', 'women_feelings',
#             'dem_feelings', 'rep_feelings', 'parental_leave')
# 
# mods <- c('pid', 'religion_prolife_recoded', 'importance_recoded', 
#           'courtlegit_recoded', 'courtknow_composite', 'news_binary',
#           'info_source_sm', 'info_source_cable', 'info_source_local', 'info_source_friend', 
#           'info_source_online', 'info_source_newspaper', 'info_source_email', 
#           'info_source_church')


#testing modelsummary
rep_model <- 
  lm_robust(moral_opinion ~ decision_cut,
            se_type = "HC1", 
            fixed_effects = ~ universal_ID,
            weights= weight_ps,
            subset = pid == 'Republican',
            data = data_panel)

dem_model <- 
  lm_robust(moral_opinion ~ decision_cut,
            se_type = "HC1", 
            fixed_effects = ~ universal_ID,
            weights= weight_ps,
            subset = pid == 'Democrat',
            data = data_panel)

ind_model <- 
  lm_robust(moral_opinion ~ decision_cut,
            se_type = "HC1", 
            fixed_effects = ~ universal_ID,
            weights= weight_ps,
            subset = pid == 'Independent',
            data = data_panel)

modelsummary(list("Republican"=rep_model, "Democrat"=dem_model, "Independent"=ind_model),
             estimate  = "{estimate}",
             fmt = function(x) round(x, 3),
             statistic = "[{conf.low}, {conf.high}], p = {p.value}",
             coef_rename = c("Ruling"),
             output = "latex")


rep_model_c <- 
  lm_robust(norm ~ decision_cut,
            se_type = "HC1", 
            weights= weight,
            subset = pid == 'Republican',
            data = data_cross)

dem_model_c <- 
  lm_robust(norm ~ decision_cut,
            se_type = "HC1", 
            weights= weight,
            subset = pid == 'Democrat',
            data = data_cross)

ind_model_c <- 
  lm_robust(norm ~ decision_cut,
            se_type = "HC1", 
            weights= weight,
            subset = pid == 'Independent',
            data = data_cross)


modelsummary(list("Republican"=rep_model_c, "Democrat"=dem_model_c, "Independent"=ind_model_c),
             estimate  = "{estimate}",
             fmt = function(x) round(x, 3),
             statistic = "[{conf.low}, {conf.high}], p = {p.value}",
             coef_rename = c("Intercept", 'Ruling'),
             output = "latex")

# regression output tables (panel, 3 wave) --------------------------------------
dvs <- 'personal_roe'
for(i in 1:length(dvs)){
  
  panel_model <- 
    lm_robust(data_panel[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              fixed_effects = ~ universal_ID,
              weights= weight_ps,
              data = data_panel)
  
  cross_model <- 
    lm_robust(data_cross[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data_cross)
  
  modelsummary(list("Panel"=panel_model, "Cross"=cross_model),
               estimate  = "{estimate}",
               fmt = function(x) round(x, 3),
               statistic = "[{conf.low}, {conf.high}], p = {p.value}",
               output = "latex")
  
  # screenreg(list(panel_model, cross_model), digits = 3, include.ci = TRUE, 
  #        file = glue("Tables/{dvs[i]}.tex"),
  #        stars = c(0.001, 0.01, 0.05),
  #        custom.model.names = c("Panel", "Cross-Sectional"), 
  #        custom.coef.names= c("Leak", "Ruling", "Intercept"), 
  #        caption= dvs[i],
  #        caption.above = TRUE,
  #        custom.note = "*$p<.05$.")
}

#lagged model for panel 
for(i in 1:length(dvs)){
  
  #lagged DV 
  lag_model <- 
    lm_robust(data_panel[,dvs[i]] ~ decision_cut + data_panel[,dv_last[i]],
              fixed_effects = ~ universal_ID,
              se_type = "HC1",
              weights = weight_ps,
              data = data_panel)
  
  texreg(list(lag_model), digits = 3, include.ci = TRUE, 
         file = glue("Tables/lagged_{dvs[i]}.tex"),
         stars = c(0.001, 0.01, 0.05),
         custom.model.names = c("Panel"), 
         custom.coef.names= c("Ruling", "Lagged DV"), 
         caption= dvs[i],
         caption.above = TRUE,
         custom.note = "*$p<.05$.")
}





# regression tables (2 waves) ---------------------------------------------

for(i in 1:length(dvs2)){
  
  panel_model <- 
    lm_robust(data_panel[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              fixed_effects = ~ universal_ID,
              weights= weight_ps,
              data = data_panel)
  
  cross_model <- 
    lm_robust(data_cross[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data_cross)
  
  screenreg(list(panel_model, cross_model), digits = 3, include.ci = TRUE, 
            file = glue("Tables/{dvs2[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            custom.model.names = c("Panel", "Cross-Sectional"), 
            custom.coef.names= c("Ruling", "Intercept"), 
            caption= dvs2[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$.")
}


# heterogenous regression output tables (panel, 3 wave) --------------------------------------

for(i in 1:length(dvs)){
  
  panel_model <- 
    lm_robust(data_panel[,dvs[i]] ~ leak_cut*pid + decision_cut*pid,
              se_type = "HC1", 
              fixed_effects = universal_ID,
              weights= weight_ps,
              data = data_panel)
  
  cross_model <- 
    lm_robust(data_cross[,dvs[i]] ~ leak_cut*pid + decision_cut*pid,
              se_type = "HC1", 
              weights= weight,
              data = data_cross)
  
  texreg(list(panel_model, cross_model), digits = 3, include.ci = TRUE, 
         file = glue("Tables/pid_{dvs[i]}.tex"),
         stars = c(0.001, 0.01, 0.05),
         custom.model.names = c("Panel", "Cross-Sectional"), 
         custom.coef.names= c("Leak", "Independent", "Republican",
                              "Ruling", "Leak:Ind", "Leak:Rep", "Ruling:Ind", "Ruling:Rep", "(Intercept)"), 
         caption= dvs[i],
         caption.above = TRUE,
         custom.note = "*$p<.05$. Comparison group: Democrats")
}


######## two-wave dvs #######

for(i in 1:length(dvs2)){
  
  panel_model <- 
    lm_robust(data_panel[,dvs2[i]] ~ decision_cut*pid,
              se_type = "HC1", 
              fixed_effects = universal_ID,
              weights= weight_ps,
              data = data_panel)
  
  cross_model <- 
    lm_robust(data_cross[,dvs2[i]] ~ decision_cut*pid,
              se_type = "HC1", 
              weights= weight,
              data = data_cross)
  
  texreg(list(panel_model, cross_model), digits = 3, include.ci = TRUE, 
         file = glue("Tables/pid_{dvs2[i]}.tex"),
         stars = c(0.001, 0.01, 0.05),
         custom.model.names = c("Panel", "Cross-Sectional"), 
         custom.coef.names= c("Ruling", "Independent", "Republican",
                              "Ruling:Ind", "Ruling:Rep", "(Intercept)"), 
         caption= dvs2[i],
         caption.above = TRUE,
         custom.note = "*$p<.05$. Comparison group: Democrats")
}


# simple slopes (three wave) -----------------------------------------------------------
#panel
for(i in 1:length(dvs)){
  
  data <- data_panel %>% filter(pid == 'Republican')
  rep_panel <- 
    lm_robust(data[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              fixed_effects = universal_ID,
              weights= weight_ps,
              data = data)
  
  data <- data_panel %>% filter(pid == 'Democrat')
  dem_panel <- 
    lm_robust(data[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              fixed_effects = universal_ID,
              weights= weight_ps,
              data = data)
  
  data <- data_panel %>% filter(pid == 'Independent')
  ind_panel <- 
    lm_robust(data[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              fixed_effects = universal_ID,
              weights= weight_ps,
              data = data)
  
  
  screenreg(list(rep_panel, dem_panel, ind_panel), digits = 3, include.ci = TRUE,
            file = glue("Tables/panel_{dvs[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            custom.model.names = c("Republicans", "Democrats", "Independents"),
            custom.coef.names= c("Leak", "Ruling"),
            caption= dvs[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$.")
}

#cross section
for(i in 1:length(dvs)){
  
  data <- data_cross %>% filter(pid == 'Republican')
  rep_cross <- 
    lm_robust(data[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data)
  
  data <- data_cross %>% filter(pid == 'Democrat')
  dem_cross <- 
    lm_robust(data[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data)
  
  data <- data_cross %>% filter(pid == 'Independent')
  ind_cross <- 
    lm_robust(data[,dvs[i]] ~ leak_cut + decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data)
  
  
  screenreg(list(rep_cross, dem_cross, ind_cross), digits = 3, include.ci = TRUE,
            file = glue("Tables/cross_{dvs[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            custom.model.names = c("Republicans", "Democrats", "Independents"),
            custom.coef.names= c("(Intercept)", "Leak", "Ruling"),
            caption= dvs[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$.")
}


# simple slopes (two waves) -----------------------------------------------

#panel
for(i in 1:length(dvs2)){
  
  data <- data_panel %>% filter(pid == 'Republican')
  rep_panel <- 
    lm_robust(data[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              fixed_effects = universal_ID,
              weights= weight_ps,
              data = data)
  
  data <- data_panel %>% filter(pid == 'Democrat')
  dem_panel <- 
    lm_robust(data[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              fixed_effects = universal_ID,
              weights= weight_ps,
              data = data)
  
  data <- data_panel %>% filter(pid == 'Independent')
  ind_panel <- 
    lm_robust(data[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              fixed_effects = universal_ID,
              weights= weight_ps,
              data = data)
  
  screenreg(list(rep_panel, dem_panel, ind_panel), digits = 3, include.ci = TRUE,
            file = glue("Tables/panel_{dvs2[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            custom.model.names = c("Republicans", "Democrats", "Independents"),
            custom.coef.names= c("Ruling"),
            caption= dvs2[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$.")
}

#cross section
for(i in 1:length(dvs2)){
  
  data <- data_cross %>% filter(pid == 'Republican')
  rep_cross <- 
    lm_robust(data[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data)
  
  data <- data_cross %>% filter(pid == 'Democrat')
  dem_cross <- 
    lm_robust(data[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data)
  
  data <- data_cross %>% filter(pid == 'Independent')
  ind_cross <- 
    lm_robust(data[,dvs2[i]] ~ decision_cut,
              se_type = "HC1", 
              weights= weight,
              data = data)
  
  
  screenreg(list(rep_cross, dem_cross, ind_cross), digits = 3, include.ci = TRUE,
            file = glue("Tables/cross_{dvs2[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            custom.model.names = c("Republicans", "Democrats", "Independents"),
            custom.coef.names= c("(Intercept)", "Ruling"),
            caption= dvs2[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$.")
}

data_panel %>% 
  filter(pid == 'Republican') %>%
  lm_robust(fetal_scale ~ decision_cut,
            se_type = "HC1", 
            fixed_effects = universal_ID,
            weights= weight_ps,
            data = .)

data_cross %>% 
  filter(pid == 'Republican') %>%
  lm_robust(fetal_scale ~ decision_cut,
            se_type = "HC1", 
            weights= weight,
            data = .)




# heterogeneity by age ----------------------------------------------------

for(i in 1:length(dvs)){
  
  panel_model <- 
    lm_robust(data_panel[,dvs[i]] ~ leak_cut*as.factor(age4) + decision_cut*as.factor(age4),
              se_type = "HC1", 
              weights= weight_ps,
              data = data_panel)
  
  cross_model <- 
    lm_robust(data_cross[,dvs[i]] ~ leak_cut*as.factor(age4) + decision_cut*as.factor(age4),
              se_type = "HC1", 
              weights= weight,
              data = data_cross)
  
  screenreg(list(panel_model, cross_model), digits = 3, include.ci = TRUE, 
            file = glue("Tables/age_{dvs[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            custom.model.names = c("Panel", "Cross-Sectional"), 
            #custom.coef.names= c("(Intercept)", "Leak", "Independent", "Republican",
            # "Ruling", "Leak:Ind", "Leak:Rep", "Ruling:Ind", "Ruling:Rep"), 
            caption= dvs[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$. Comparison group: Democrats")
}
